<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\db\Expression;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }


    public function getSeasonShowYearList()
    {
        $year = model('app\common\model\seasonshow\YearModel')
            ->where(['status' => '1'])
            ->order('weigh','asc')
            ->select();
        $this->success('请求成功',$year,200);
    }


    public function getSeasonShowProductList()
    {
        $id = $this->request->request('id');
        $year_id = $this->request->request('year');
        $product_where = [
            'status' => '1',
            'season_show_id' => $id,
        ];
        if(!$year_id) {
            $year_id = model('app\common\model\seasonshow\YearModel')
                ->where(['status' => '1'])
                ->order('weigh','asc')
                ->value('id');
        }
        $product_where['year_id'] = $year_id;
        $product = model('app\common\model\seasonshow\ProductModel')
            ->where($product_where)
            ->order('weigh','asc')
            ->select();
        foreach ($product as $row) {
            $row->image1 = full_url($row->image1);
            $row->image2 = full_url($row->image2);
        }
        $this->success('请求成功',$product,200);
    }


    public function sendResume()
    {
        $file = $this->request->request('file');
        $res = model('app\admin\model\join\SendResume')->create([
            'file' => $file
        ]);
        if($res) {
            $this->success('上传成功',[],200);
        }
        $this->error('上传失败');
    }


    public function sendBusiness()
    {
        $name = $this->request->post('name');
        $mobile = $this->request->post('mobile');
        $email = $this->request->post('email');
        $content = $this->request->post('content');
        if(!$name || !$mobile || !$email || !$content) {
            $this->error('参数错误');
        }

        $res = model('app\admin\model\Business')->create([
            'name' => $name,
            'mobile' => $mobile,
            'email' => $email,
            'content' => $content,
        ]);
        if($res) {
            $this->success('提交成功',[],200);
        }
        $this->error('提交失败');
    }


    public function sendMessage()
    {
        $name = $this->request->post('name');
        $mobile = $this->request->post('mobile');
        $email = $this->request->post('email');
        $content = $this->request->post('content');
        if(!$name || !$mobile || !$email || !$content) {
            $this->error('参数错误');
        }

        $res = model('app\admin\model\Message')->create([
            'name' => $name,
            'mobile' => $mobile,
            'email' => $email,
            'content' => $content,
        ]);
        if($res) {
            $this->success('提交成功',[],200);
        }
        $this->error('提交失败');
    }


    public function getShopArea()
    {
        $province = $this->request->request('province','');
        $exp = new Expression('convert(city using gbk) asc');
        $where = [
            'status' => '1'
        ];
        if(!empty($province)){
            $where['province'] = $province;
        }
        $data = model('app\admin\model\shop\Shop')
            ->where($where)
            ->whereNotNull('lat')
            ->orderRaw($exp)
            ->group('city')
            ->column('city');
//        if($province) {
//            $exp = new Expression('convert(province using gbk) asc');
//            $data = model('app\admin\model\shop\Shop')
//                ->where(['status' => '1'])
//                ->whereNotNull('lat')
//                ->orderRaw($exp)
//                ->group('province')
//                ->column('province');
//        }else{
//            $exp = new Expression('convert(city using gbk) asc');
//            $data = model('app\admin\model\shop\Shop')
//                ->where(['status' => '1'])
//                ->whereNotNull('lat')
//                ->orderRaw($exp)
//                ->group('city')
//                ->column('city');
//        }
        $this->success('请求成功',$data,200);
    }

    public function getProductPageList()
    {
        $id = $this->request->request('id');
        if(!$id) $this->error('数据错误');
        $page = $this->request->request('page',1);
        $limit = $this->request->request('limit',8);
        $data = model('app\admin\model\NewProductDetail')
            ->field('id,new_product_id,name,image,images')
            ->where(['status' => '1','new_product_id' => $id])
            ->order('weigh','asc')
            ->paginate($limit, false, ['page' => $page]);
        if(!$data) $this->error('数据错误');
        foreach ($data as $row) {
            $row->images = full_url($row->images,1);
        }
        $this->success('请求成功',$data,200);
    }

}
